package jun;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020-6-2 10:49
 * @description：
 * @modified By：
 * @version:
 */
public class Jun02 {
    /**
     * create by: 冯涛滔
     * description: https://leetcode-cn.com/problems/minimum-swaps-to-make-strings-equal/ 交换字符串使字符串相同
     * create time: 2020-6-2 11:02
     * @params [s1, s2]
     * @return int
     */
    public int minimumSwap(String s1, String s2) {
        int len = s1.length(), xy = 0, yx = 0;
        char[] cs1 = s1.toCharArray();
        char[] cs2 = s2.toCharArray();
        for(int i = 0; i < len; i ++) {
            if(cs1[i] == cs2[i]) {
                continue;
            }
            else if(cs1[i] == 'x') {
                xy ++;
            }
            else {
                yx ++;
            }
        }
        if ((xy + yx) % 2 != 0) {
            return -1;
        }
        /* xyCnt + yxCnt 是一个偶数，且xyCnt与yxCnt都不是0，那么可能有“奇数+奇数”和“偶数+偶数”两种情况 */
        //基于上面的分析，可以发现每2个xyCnt或yxCnt对应一次移动，1个xyCnt与1个yxCnt对应两次移动，详见代码中注释下面那段代码；
        if (xy % 2 != 0) {
            return xy / 2 + yx / 2 + 2; //+2就是因为奇数加奇数会剩下一个xy+yx
        } else {
            return xy / 2 + yx / 2;
        }
    }
    /**
     * create by: 冯涛滔
     * description: https://leetcode-cn.com/problems/delete-columns-to-make-sorted/submissions/ 删列造序
     * create time: 2020-6-2 11:01
     * @params [A]
     * @return int
     */
    public int minDeletionSize(String[] A) {
        int ans = 0;
        for (int c = 0; c < A[0].length(); ++c)
            for (int r = 0; r < A.length - 1; ++r)
                if (A[r].charAt(c) > A[r+1].charAt(c)) {
                    ans++;
                    break;
                }

        return ans;
    }
    /**
     * create by: 冯涛滔
     * description: https://leetcode-cn.com/problems/cinema-seat-allocation/submissions/ 安排电影院座位
     * 这个神仙题做位运算的
     * create time: 2020-6-2 11:06
     * @params [n, reservedSeats]
     * @return int
     */
    public static int maxNumberOfFamilies(int n, int[][] reservedSeats) {
        Arrays.sort(reservedSeats, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        int res = n*2;//假设都能放下两排
        int j = 0;
        for (int i = 1; i <= n ; i++) {//遍历每一条
            boolean left = true;
            boolean right = true;
            boolean midLeft = true;
            boolean midRight = true;
            while(reservedSeats[j][0]==i){
                int a = reservedSeats[j][1];
                switch(a){
                    case 2:
                    case 3:
                        left = false;
                        break;
                    case 4:
                    case 5:
                        midLeft = false;
                        break;
                    case 6:
                    case 7:
                        midRight = false;
                        break;
                    case 8:
                    case 9:
                        right = false;
                        break;
                }
                j++;
            }
            int t = 0;
            if(left&&right){
                continue;
            }else {
                if(!left||!midLeft){
                    t++;
                }
                if(!right||!midRight){
                    t++;
                }
                if(t==2&midLeft&midRight){
                    t--;
                }
            }
            res-=t;
        }
        return res;
    }


}
